﻿using System;
using Microsoft.DirectX.DirectInput;

namespace WolfGame {

  /// <summary>
  /// This class will read the current keyboard state and route
  /// keys to their appropriate destinations
  /// </summary>
  class c_Keyboard {

    #region Constants

    private const float TURN_SPEED = .04f;

    #endregion

    #region Member Variables

    private Common common;
    private Boolean sprint;                                     //is the player sprinting?

    #endregion

    #region Constructor

    /// <summary>
    /// Instantiate the class and grab the keyboard
    /// </summary>
    /// <param name="common"></param>
    public c_Keyboard(Common common) {
      this.common = common;

      Keyboard = new Microsoft.DirectX.DirectInput.Device(SystemGuid.Keyboard);
      Keyboard.SetCooperativeLevel(common.Render, CooperativeLevelFlags.Background
          | CooperativeLevelFlags.NonExclusive);
      Keyboard.Acquire();
      Console.WriteLine("Initializing keyboard...");

      // The camera should default to the player - change it
      CameraOnPlayer = false;
    }

    #endregion

    #region Events

    public void ReadKeyboard() {
      float vPerCall = 0;
      float hPerCall = 0;
      KeyboardState keys = Keyboard.GetCurrentKeyboardState();

      // Rotating camera right.
      if (keys[Key.Right]) {
        //hPerCall = -TURN_SPEED;
        hPerCall = -common.Player.TurnSpeed;
        if (CameraOnPlayer)
          common.Player.HRadians += hPerCall;
        else
          common.Camera.rotateCamera(hPerCall, 0);
      }
      // Rotating camera left.
      if (keys[Key.Left]) {
        //hPerCall = TURN_SPEED;
        hPerCall = common.Player.TurnSpeed;
        if (CameraOnPlayer)
          common.Player.HRadians += hPerCall;
        else
          common.Camera.rotateCamera(hPerCall, 0);
      }
      // Rotating camera up.
      if (keys[Key.R]) {
        vPerCall = TURN_SPEED;
        if (CameraOnPlayer)
          common.Player.VRadians += vPerCall;
        else
          common.Camera.rotateCamera(0, vPerCall);
      }
      // Rotating camera down.
      if (keys[Key.V]) {
        vPerCall = -TURN_SPEED;
        if (CameraOnPlayer)
          common.Player.VRadians += vPerCall;
        else
          common.Camera.rotateCamera(0, vPerCall);
      }
      // Move player forward in whatever direction it is facing
      if (keys[Key.UpArrow]) {
        //camera.moveCamera(.25f);
        if (CameraOnPlayer)
          common.Player.MoveFrontOrBack(sprint, -1);
        else
          common.Camera.moveCamera(.25f);
      }
      //Move player backward in whatever direction it is facing
      if (keys[Key.DownArrow]) {
        if (CameraOnPlayer)
          common.Player.MoveFrontOrBack(sprint, 1);
        else
          common.Camera.moveCamera(-.25f);
      }
      //Center the view
      if (keys[Key.G]) {
        //player.centerView();
        common.Player.VRadians = 0f;
      }
      //Slide camera left (strafe)
      if (keys[Key.A]) {
        if (CameraOnPlayer)
          common.Player.strafe(sprint, -1);
        else
          common.Camera.slideCamera(.25f, 0);
      }
      //Slide camera right (strafe)
      if (keys[Key.S]) {
        if (CameraOnPlayer)
          common.Player.strafe(sprint, 1);
        else
          common.Camera.slideCamera(-.25f, 0);
      }
      //Slide camera up
      if (keys[Key.Q])
        common.Camera.slideCamera(0, .25f);
      //Slide camera down
      if (keys[Key.Z])
        common.Camera.slideCamera(0, -.25f);
      if (keys[Key.P]) {
        common.Player.Camera = common.Camera;
        CameraOnPlayer = true;
      }
      if (keys[Key.O]) {
        common.Player.Camera = null;
        CameraOnPlayer = false;
      }
      if (keys[Key.LeftShift])
        sprint = true;
      else
        sprint = false;

      // Close the program
      if (keys[Key.Escape]) {
        common.Render.CloseWindow(true);
      }
    }

    #endregion

    #region Properties

    public Microsoft.DirectX.DirectInput.Device Keyboard { get; set; }
    public bool CameraOnPlayer { get; set; }

    #endregion
  }
}
